package com.conan.dailyflickr.app;

import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class DailyPrefernces {
	private static final String PREFS_NAME = "dailyPhotoPref";
	public static final String PREFS_NAME_PAGE = "daily_page";
	public static final String PREFS_NAME_DATE = "daily_date";
	public static final String PREFS_NAME_MAIN_ID = "daily_id";
	public static final String PREFS_NAME_URL = "daily_url";
	public static final String PREFS_NAME_TITLE = "daily_title";
	public static final String PREFS_NAME_SET_WALLPAPER = "daily_set_wallpaper";
	static Context context;
	
    public static DailyInfo getPref(Context context){
       SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
       
	   
       int page = settings.getInt(PREFS_NAME_PAGE, 1);
       long date = settings.getLong(PREFS_NAME_DATE, 0);
       String url = settings.getString(PREFS_NAME_URL, null);
       String title = settings.getString(PREFS_NAME_TITLE, null);
       
       Calendar cal = Calendar.getInstance();
       if(date == 0){    	   
    	   cal.add(Calendar.DATE, -2);
       }else{
    	   cal.setTimeInMillis(date);
       }
    	  
       return new DailyInfo(page, cal, url, title);
      
    }

    public static void putPref(Context context, int page, Calendar cal){
      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);;
      SharedPreferences.Editor editor = settings.edit();
      if(page >= 0){
    	  editor.putInt(PREFS_NAME_PAGE, page);
      }
      editor.putLong(PREFS_NAME_DATE, cal.getTimeInMillis());

      // Commit the edits!
      editor.commit();
    }
    
    public static void putPref(Context context, String url, String title){
        // We need an Editor object to make preference changes.
        // All objects are from android.context.Context
        SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);;
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(PREFS_NAME_URL, url);
        editor.putString(PREFS_NAME_TITLE, title);

        // Commit the edits!
        editor.commit();
      }
    
    public static class DailyInfo{
    	public int _page;
    	public Calendar _date;
    	public String _url;
    	public String _title;
    	

    	public DailyInfo(int page, Calendar date, String url, String title) {
    		_page = page;
    		_date = date;
    		_url = url;
    		_title = title;
		}

    	public void setInfo(DailyInfo info) {
    		_page = info._page;
    		_date = info._date;
    		_url = info._url;
    		_title = info._title;
		}    	
    }

}
